<script setup lang="ts">
import { ref, watch } from "vue";
import { onLoad, onPageScroll, onPullDownRefresh, onReachBottom } from "@dcloudio/uni-app";
import { getCollectList } from "@/api/user";
import { useInfiniteScroll } from "@/composables/useInfiniteScroll";
import { handleToPage } from "@/utils";

const { items: list, isLoading, hasMore, error, resetAndLoad, loadMore } = useInfiniteScroll(getCollectList);

const tabs = [
  { key: '1', label: '活动'},
  { key: '2', label: '兑换'},
];

/** 查询参数  */
const queryParams = ref({
  type: '1'
});

watch(queryParams, (newQueryParams) => {
  console.log("Query Params changed:", newQueryParams);
  resetAndLoad(newQueryParams);
}, { deep: true });

// 页面加载
onLoad(() => {
  resetAndLoad(queryParams.value);
});

// 监听用户下拉动作
onPullDownRefresh(() => {
  resetAndLoad(queryParams.value).finally(() => {
    // 停止当前页面下拉刷新
    uni.stopPullDownRefresh();
  });
});

// 页面滚动到底部的事件
onReachBottom(() => loadMore());

// 吸顶变色
const isScrolled = ref(false);
onPageScroll((e)=>{
  isScrolled.value = e.scrollTop > 0;
});
</script>

<template>
<view class="relative overflow-hidden">
  <page-navbar title="我的收藏" :hideTitle="true" :isScrolled="isScrolled"></page-navbar>

  <page-title title="我的收藏"></page-title>
  <!--  
  <view class="my-1 overflow-x-auto scrollbar-hide">
    <view class="w-fit flex gap-2 p-4">
      <view
        class="rounded-full px-3 py-1 box-border break-keep text-sm"
        :class="[queryParams.type == item.key ? 'text-white bg-gradient-to-r from-#5199F9 to-#0164E6' : 'text-black/80 bg-white border-(~ solid black/8)']"
        v-for="(item, index) in tabs" :key="index"
        @click="queryParams.type = item.key"
      >
        {{ item.label }}
      </view>
    </view>
  </view>
  -->
<!--  
  <view class="bg-white rounded-lg p-3 mb-5 mx-4 grid gap-2">
    <image class="w-full h-45 align-middle rounded-md" src="https://picsum.photos/320/180" mode="aspectFill" />
    <view class="text-(sm black/80) truncate">郡主的下午茶</view>
    <view class="text-(sm black/60) line-clamp-2">每周一期，十人以内深度小范围交流，欢迎律师、法务等报名参加，会员优先。</view>
    <view class="flex items-center justify-between pt-2 border-t-(~ solid black/8)">
      <view class="text-(sm black/60)">2025年1月10日 · 北京 · 免费</view>
      <view class="text-(sm/6 white) px-3 rounded-full bg-gradient-to-r from-#FF920D to-#FFCF0D">报名参加</view>
    </view>
  </view>
-->
  <page-list :list="list" :hasMore="hasMore" :isLoading="isLoading" :loadMore="loadMore">
    <view class="grid gap-5 mt-5" v-if="queryParams.type == '1'">
      <view class="bg-white rounded-lg p-3 mx-4 grid gap-2 intrinsic-h-80" v-for="item in list" :key="item.id" @click="handleToPage(`/pages/activity/detail?id=${item.id}`)">
        <image class="w-full h-45 align-middle rounded-md" :src="`https://api.minglvtang.com${item.imgs}`" :fade-in="true" :lazy-load="true" mode="widthFix" />
        <view class="text-(sm black/80) font-500 truncate">{{ item.title }}</view>
        <view class="text-(sm black/60) line-clamp-2">{{ item.desc }}</view>
        <view class="text-(sm black/60) pt-2 border-t-(~ solid black/8)">{{item.start_time}} · {{item.city}} · {{ item.price == 0 ? '免费' : item.price }}</view>
      </view>
    </view>
    <view class="mx-4 grid gap-3 grid-cols-2" v-if="queryParams.type == '2'">
      <view class="bg-white rounded-lg pb-3 grid gap-2 shadow" v-for="item in list" :key="item.id" @click="handleToPage(`/pages/goods/detail?id=${item.id}`)">
        <image class="w-full h-40 align-middle rounded-t-md bg-black" :src="`https://api.minglvtang.com${item.imgs}`" :fade-in="true" :lazy-load="true" mode="aspectFill" />
        <view class="mx-3 text-(sm black/80) font-500 truncate">{{ item.title }}</view>
        <view class="mx-3 text-(sm #EA3E3E) pt-2 border-t-(~ solid black/8)">{{ item.price == 0 ? '免费' : item.price }}</view>
      </view>
    </view>
  </page-list>
</view>
</template>
